package com.czh.subject.application.config;

import com.czh.subject.application.Interceptor.LoginInterceptor;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;
import reactor.util.annotation.NonNull;

import java.util.List;
import java.util.TreeMap;
import java.util.Vector;

/**
 * @author caozaihui
 * @version 1.0
 * @description: mvc全局配置 TODO tip
 * @since 2024/8/14 下午 11:50
 */
@Configuration
public class MVCGlobalConfig extends WebMvcConfigurationSupport {

    /**
     * 添加自定义拦截器
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**");
    }

    @Override
    public void configureMessageConverters( @NonNull List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        converters.add(createMappingJackson2HttpMessageConverter());
    }
    /**
     * 全局消息转换器
     * @return HttpMessageConverter
     */
    private HttpMessageConverter<?> createMappingJackson2HttpMessageConverter() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return new MappingJackson2HttpMessageConverter(mapper);
    }
}
